﻿using System;
using AutoMapper;
using Enferno.Public.InversionOfControl;
using Enferno.Public.Web.Builders;
using Enferno.Public.Web.Mappers.BasketProfiles;
using Enferno.Public.Web.Mappers.ProductProfiles;
using Enferno.Public.Web.Mappers.PromotionProfiles;
using Enferno.Public.Web.Models;
using Enferno.StormApiClient.Shopping;
using Enferno.Web.StormUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

namespace Enferno.Public.Web.Test.Extending
{
    [TestClass]
    public class ExtendedBasketTests
    {
        [TestInitialize]
        public void Initialize()
        {
            var rules = MockRepository.GenerateMock<ISiteRules>();
            IoC.RegisterInstance(typeof(ISiteRules), rules);

            Mapper.Configuration.AddProfile<BasketToBasketModelProfile>();
            Mapper.Configuration.AddProfile<BasketItemModelToBasketItemProfile>();
            Mapper.Configuration.AddProfile<BasketModelToBasketProfile>();
            Mapper.Configuration.AddProfile<BasketItemToBasketItemModelProfile>();

            Mapper.Configuration.AddProfile<PromotionProfile>();
        }

        //**************************************************** Test 1 ****************************************************
        //Extend basket model with status id

        class BasketWithStatusIdModel : BasketModel
        {
            public int StatusId { get; set; }
        }

        class ExtendedBasketBuilder :
            BasketBaseBuilder
                <BasketWithStatusIdModel, BasketItemModel, PromotionModel, OnHandStatusModel, PriceModel,
                    ProductFileModel>
        {
            public ExtendedBasketBuilder(ISiteRules rules) : base(rules)
            {
                
            }
        }

        class ExtendedBasketToBasketModelMapProfile :
            BasketToBasketBaseModelProfile
                <BasketWithStatusIdModel, BasketItemModel, PromotionModel, OnHandStatusModel, PriceModel,
                    ProductFileModel>
        {
            
        }

        [TestMethod]
        public void CanBuildBasketWithExtendedBAsketModel()
        {
            var rules = MockRepository.GenerateMock<ISiteRules>();

            //add the extended mapping needed to map the new property in the extended product model
            Mapper.Configuration.AddProfile<ExtendedBasketToBasketModelMapProfile>();

            var basket = new Basket
            {
                Items = new BasketItemList(),
                AppliedPromotions = new PromotionList(),
                StatusId = 1
            };

            var builder = new ExtendedBasketBuilder(rules);

            var stormContext = MockRepository.GenerateMock<IStormContext>();
            stormContext.Stub(x => x.Configuration).Return(new StormConfigurationSection());
            stormContext.Stub(x => x.ShowPricesIncVat).Return(false);
            stormContext.Stub(x => x.CultureCode).Return("sv");
            StormContext.SetInstance(stormContext);

            var model = builder.MapBasketModel(basket);

            Assert.AreEqual(basket.StatusId, model.StatusId);
        }
    }
}
